package com.ruoyi.merchant.controller;

import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.merchant.domain.dto.CouponCreateDTO;
import com.ruoyi.merchant.domain.dto.CouponQueryDTO;
import com.ruoyi.merchant.domain.dto.CouponUpdateDTO;
import com.ruoyi.merchant.domain.vo.CouponStatisticsVO;
import com.ruoyi.merchant.domain.vo.CouponVO;
import com.ruoyi.merchant.service.ICouponManagementService2;

/**
 * 优惠券管理Controller
 * 
 * @author xperia
 */
@RestController
@RequestMapping("/merchant/coupon")
public class CouponManagementController2 extends BaseController {
    
    @Autowired
    private ICouponManagementService2 couponManagementService;

    /**
     * 查询优惠券列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CouponQueryDTO queryDTO) {
        startPage();
        List<CouponVO> list = couponManagementService.selectCouponList(queryDTO);
        return getDataTable(list);
    }

    /**
     * 查询优惠券详情
     */
    @GetMapping("/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") String couponId) {
        return success(couponManagementService.selectCouponById(couponId));
    }

    /**
     * 新增优惠券
     */
    @Log(title = "优惠券管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody CouponCreateDTO createDTO) {
        return toAjax(couponManagementService.insertCoupon(createDTO));
    }

    /**
     * 修改优惠券
     */
    @Log(title = "优惠券管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody CouponUpdateDTO updateDTO) {
        return toAjax(couponManagementService.updateCoupon(updateDTO));
    }

    /**
     * 删除优惠券
     */
    @Log(title = "优惠券管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponId}")
    public AjaxResult remove(@PathVariable String couponId) {
        return toAjax(couponManagementService.deleteCoupon(couponId));
    }

    /**
     * 发布优惠券
     */
    @Log(title = "发布优惠券", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{couponId}")
    public AjaxResult publish(@PathVariable String couponId) {
        return toAjax(couponManagementService.publishCoupon(couponId));
    }

    /**
     * 暂停优惠券
     */
    @Log(title = "暂停优惠券", businessType = BusinessType.UPDATE)
    @PutMapping("/pause/{couponId}")
    public AjaxResult pause(@PathVariable String couponId) {
        return toAjax(couponManagementService.pauseCoupon(couponId));
    }

    /**
     * 恢复优惠券
     */
    @Log(title = "恢复优惠券", businessType = BusinessType.UPDATE)
    @PutMapping("/resume/{couponId}")
    public AjaxResult resume(@PathVariable String couponId) {
        return toAjax(couponManagementService.resumeCoupon(couponId));
    }

    /**
     * 获取优惠券统计数据
     */
    @GetMapping("/statistics")
    public AjaxResult statistics() {
        CouponStatisticsVO statistics = couponManagementService.getCouponStatistics();
        return success(statistics);
    }
}

